package com.rongc.client.freight.base.view.adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Checkable;

import java.util.List;

/**
 * Created by Bruce on 2016/12/2.
 */
public abstract class RecyclerChoiceBaseAdapter<T> extends RecyclerBaseAdapter<T> {
    protected SparseBooleanArray sp;  // 存储选择数据的数据结构
    private SparseBooleanArray spTemp;  // 缓存筛选条件的变量，常用于取消操作
    protected String key;
    protected int choiseMode  = AbsListView.CHOICE_MODE_MULTIPLE;
    private OnItemClickListener choiceListener;
    public RecyclerChoiceBaseAdapter(Context mContext, List<T> datas){
        super(mContext,datas);
        sp = new SparseBooleanArray();
        // 每次选择后即时保存到sp中
        setOnItemClickListener(new OnItemClickListener<T>() {
            @Override
            public void onItemClick(View view, int position, T data) {
                if(!isCheckable(position)) return;
                if(choiseMode == AbsListView.CHOICE_MODE_SINGLE){
                    for (int i = 0; i < sp.size(); i++) {
                        int key = sp.keyAt(i);
                        if(position == key)
                            if(sp.get(key)) return;
                    }
                    sp.clear();
                    ((Checkable)view).toggle();
                    sp.put(position,((Checkable)view).isChecked());
                    notifyDataSetChanged();
                    if(choiceListener != null) choiceListener.onItemClick(view,position,data);
                }else if(choiseMode == AbsListView.CHOICE_MODE_MULTIPLE){
                    ((Checkable)view).toggle();
                    sp.put(position,((Checkable)view).isChecked());
                    if(choiceListener != null) choiceListener.onItemClick(view,position,data);
                }else{

                }
            }
        });
    }

    public String getKey() {
        return key;
    }

    public boolean isCheckable(int position){
        return true;
    }

    public void setChoiceListener(OnItemClickListener choiceListener) {
        this.choiceListener = choiceListener;
    }

    public void syncSP(){
        spTemp = getSp();
    }

    public void makeCache(){
        spTemp = getSp().clone();
    }

    public void rollback(){
        sp = spTemp;
        notifyDataSetChanged();
    }


    public SparseBooleanArray getSp(){
        return sp;
    }

    public void setSp(SparseBooleanArray sp){
        if(sp == null)
            this.sp = new SparseBooleanArray();
        else
            this.sp = sp;
    }

    public void setItemChecked(int position,boolean value){
        if(choiseMode == AbsListView.CHOICE_MODE_SINGLE){
            for (int i = 0; i < sp.size(); i++) {
                int key = sp.keyAt(i);
                if(position == key)
                    if(sp.get(key)) return;
            }
            sp.clear();
            sp.put(position,value);
            notifyDataSetChanged();
        }else if(choiseMode == AbsListView.CHOICE_MODE_MULTIPLE){
            sp.put(position,value);
            notifyDataSetChanged();
        }
    }

    public void allChecked(){
        for (int i=0;i < getItemCount();i++){
            sp.put(i,true);
        }
    }

    public void cleanChecked(){
        sp.clear();
        notifyDataSetChanged();
    }

    public boolean isItemChecked(int position){
        for (int i = 0; i < sp.size(); i++) {
            int key = sp.keyAt(i);
            if(position == key)
                return sp.get(position);
        }
        return false;
    }

    public void setChoiseMode(int choiseMode){
        this.choiseMode = choiseMode;
    }

    public int getChoiseMode(){
        return this.choiseMode;
    }


}

